package spring.cloud.tasks.api.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.NameSpace;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.NameSpaceConfiguration;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.ZooKeeperCluster;
import spring.cloud.tasks.api.service.ZooKeeperClusterService;
import spring.cloud.tasks.api.utils.Response;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/console/nameSpaces")
public class NameSpaceController {

    @Autowired
    private ZooKeeperClusterService zooKeeperService;


    @RequestMapping(value = "/createNameSpace", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> createNameSpace(@RequestBody Map<String, Object> reqParams, HttpServletRequest request)
            throws RuntimeException {
        try {
            NameSpace nameSpace = new NameSpace();
            nameSpace.setZooKeeperClusterKey((String) reqParams.get("zooKeeperClusterKey"));
            nameSpace.setNameSpace((String) reqParams.get("nameSpace"));
            nameSpace.setContent("");
            String userId = (String) request.getSession().getAttribute("userId");
            zooKeeperService.createNameSpace(nameSpace, userId);
            return new ResponseEntity<>(HttpStatus.CREATED);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    @RequestMapping(value = "/deleteNameSpace", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> deleteNameSpace(@PathVariable("nameSpace") String nameSpace) throws RuntimeException {
        try {

            zooKeeperService.deleteNameSpace(nameSpace);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @RequestMapping(value = "/getNameSpace", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> getNameSpace(@PathVariable("nameSpace") String nameSpace) throws RuntimeException {
        HttpHeaders httpHeaders = new HttpHeaders();
        try {

            NameSpace nameSpace1 = zooKeeperService.getNameSpace(nameSpace);
            return new ResponseEntity<Object>(nameSpace1, httpHeaders, HttpStatus.OK);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    @RequestMapping(value = "/getOnlineNameSpaceList", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response getOnlineNameSpaceList(final HttpServletRequest request)
            throws RuntimeException {
        List<String> nameSpaceList = new ArrayList<>();
        List<String> temp = zooKeeperService.getOnlineNameSpaceList();
        if (temp != null) {
            nameSpaceList.addAll(temp);
        }
        return Response.success(nameSpaceList);
    }

    @RequestMapping(value = "/getNameSpaceConfigurationByNameSpace", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response getNameSpaceConfigurationByNameSpace(final HttpServletRequest request, @PathVariable String nameSpace)
            throws RuntimeException {
        NameSpaceConfiguration nameSpaceConfiguration = zooKeeperService.getNameSpaceConfiguration(nameSpace);
        if (nameSpaceConfiguration == null) {
            throw new RuntimeException("该域名（" + nameSpace + "）不存在");
        }
        return Response.success(nameSpaceConfiguration);
    }

    /**
     * 获取所有域列表
     */

    @GetMapping(value = "/getNameSpaceConfigurationList")
    public Response getNameSpaceConfigurationList() {
        List<NameSpaceConfiguration> nameSpaceConfigurationList = new ArrayList<NameSpaceConfiguration>();
        List<ZooKeeperCluster> zooKeeperClusterList = zooKeeperService.getZooKeeperClusterList();
        for (ZooKeeperCluster zooKeeperCluster : zooKeeperClusterList) {
            if (!zooKeeperCluster.isOffline()) {
                nameSpaceConfigurationList.addAll(zooKeeperCluster.getNameSpaceConfigurationList());
            } else {
                List<NameSpaceConfiguration> list = zooKeeperService.getNameSpaceConfigurationList(zooKeeperCluster);
                if (list != null && !list.isEmpty()) {
                    nameSpaceConfigurationList.addAll(list);
                }
            }
        }

        return Response.success(nameSpaceConfigurationList);
    }


}
